﻿using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TomatoClock.Models
{
    public class Ranking
    {
        public List<User> userList { get; set; }
        public Ranking()
        {
            userList = new List<User>();
        }
        //以日专注时间榜为例
        public List<User> GetFocusTimeDailyRankings(List<User> userList)
        {

            userList.Sort((a, b) => b.data.FocusTime.CompareTo(a.data.FocusTime));

            // 动态计算排名
            for (int i = 0; i < userList.Count; i++)
            {
                userList[i].rank = i + 1;
            }

            return userList;
        }

        public int GetRankByUserName(List<User> userList, string userName)
        {
            var sortedList = GetFocusTimeDailyRankings(userList);
            var user = sortedList.Find(u => u.UserName == userName);
            if (user != null)
            {
                return user.rank;
            }
            else
            {
                return -1; // 用户未找到
            }
        }

        public string GetUserNameByRank(List<User> userList, int rank)
        {
            var sortedList = GetFocusTimeDailyRankings(userList);
            var user = sortedList.Find(u => u.rank == rank);
            if (user != null)
            {
                return user.UserName;
            }
            else
            {
                return "Unknown"; // 排名未找到对应用户
            }
        }
        //周专注时间榜
        /*
        public List<User> GetFocusTimeWeeklyRankings()
        {
            return Users.OrderByDescending(u => u.data.FocusTimeWeekly).ToList();
        }

        public int GetFocusTimeWeeklyUserRank(string username)
        {
            var rankings = GetFocusTimeWeeklyRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowFocusTimeWeeklyRankings()
        {
            var rankings = GetFocusTimeWeeklyRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.FocusTimeWeekly} hours");
            }
        }
        //总专注时间榜
        public List<User> GetFocusTimeSumRankings()
        {
            return Users.OrderByDescending(u => u.data.FocusTimeSum).ToList();
        }

        public int GetFocusTimeSumUserRank(string username)
        {
            var rankings = GetFocusTimeSumRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowFocusTimeSumRankings()
        {
            var rankings = GetFocusTimeSumRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.FocusTimeSum} hours");
            }
        }
        //日完成任务数量榜
        public List<User> GetTaskNumDailyRankings()
        {
            return Users.OrderByDescending(u => u.data.TaskNum).ToList();
        }

        public int GetTaskNumDailyUserRank(string username)
        {
            var rankings = GetTaskNumDailyRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowTaskNumDailyRankings()
        {
            var rankings = GetTaskNumDailyRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.TaskNum} hours");
            }
        }
        //周完成任务数量榜
        public List<User> GetTaskNumWeeklyRankings()
        {
            return Users.OrderByDescending(u => u.data.TaskNumWeekly).ToList();
        }

        public int GetTaskNumWeeklyUserRank(string username)
        {
            var rankings = GetTaskNumWeeklyRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowTaskNumWeeklyRankings()
        {
            var rankings = GetTaskNumWeeklyRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.TaskNumWeekly} hours");
            }
        }
        //总完成任务数量榜
        public List<User> GetTaskNumSumRankings()
        {
            return Users.OrderByDescending(u => u.data.TaskNumSum).ToList();
        }

        public int GetTaskNumSumUserRank(string username)
        {
            var rankings = GetTaskNumSumRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowTaskNumSumRankings()
        {
            var rankings = GetTaskNumSumRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.TaskNumSum} hours");
            }
        }
        //日收成数量榜
        public List<User> GetTomatoNumDailyRankings()
        {
            return Users.OrderByDescending(u => u.data.TomatoNum).ToList();
        }

        public int GetTomatoNumDailyUserRank(string username)
        {
            var rankings = GetTomatoNumDailyRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowTomatoNumDailyRankings()
        {
            var rankings = GetTomatoNumDailyRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.TomatoNum} hours");
            }
        }
        //周收成数量榜
        public List<User> GetTomatoNumWeeklyRankings()
        {
            return Users.OrderByDescending(u => u.data.TomatoNumWeekly).ToList();
        }

        public int GetTomatoNumWeeklyUserRank(string username)
        {
            var rankings = GetTomatoNumWeeklyRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowTomatoNumWeeklyRankings()
        {
            var rankings = GetTomatoNumWeeklyRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.TomatoNumWeekly} hours");
            }
        }
        //总收成数量榜
        public List<User> GetTomatoNumSumRankings()
        {
            return Users.OrderByDescending(u => u.data.TomatoNumSum).ToList();
        }

        public int GetTomatoNumSumUserRank(string username)
        {
            var rankings = GetTomatoNumSumRankings();
            var user = rankings.FindIndex(u => u.UserName == username);
            return user + 1;
        }

        public void ShowTomatoNumSumRankings()
        {
            var rankings = GetTomatoNumSumRankings();
            foreach (var user in rankings)
            {
                Console.WriteLine($"{user.UserName}: {user.data.TomatoNumSum} hours");
            }
        }*/
    }
}
